clc,clear;
% 文件夹路径
folder_path = 'C:\Users\qx\Desktop\Training and testing datasets\Training Set';  % 替换为你的文件夹路径
[donors,acceptors] = extract_data(folder_path);
folder_path ='C:\Users\qx\Desktop\Training and testing datasets\Testing Set';  % 替换为你的文件夹路径
[donors_2,acceptors_2] = extract_data(folder_path);
folder_path = 'C:\Users\qx\Desktop\Training and testing datasets\Training Set';
[donors_false,acceptors_false] = extract_false_data(folder_path);

%%数据清洗，将所有含特殊碱基的序列清除
j = 1;
for i = 1:length(donors)
    reg = regexp(donors(i,1),'[acgt]','match');
    if(length(reg{1}) == 18)
        donors_new(j,1) = donors(i);
        j = j+1;
    end
end
donors = donors_new;

donors_2 = lower(donors_2);
j = 1;
for i = 1:length(donors_2)
    reg = regexp(donors_2(i,1),'[acgt]','match');
    if(length(reg{1}) == 18)
        donors_2_new(j,1) = donors_2(i);
        j = j+1;
    end
end
donors_2 = donors_2_new;

j = 1;
for i = 1:length(acceptors)
    reg = regexp(acceptors(i,1),'[acgt]','match');
    if(length(reg{1}) == 36)
        acceptors_new(j,1) = acceptors(i);
        j = j+1;
    end
end
acceptors = acceptors_new;

acceptors_2 = lower(acceptors_2);
j = 1;
for i = 1:length(acceptors_2)
    reg = regexp(acceptors_2(i,1),'[acgt]','match');
    if(length(reg{1}) == 36)
        acceptors_2_new(j,1) = acceptors_2(i);
        j = j+1;
    end
end
acceptors_2 = acceptors_2_new;
%%

[chi2_values,donors_value,chi2_values_2,acceptors_value] = chi_calculate(donors,acceptors);
[chi2_values_3,donors_false_value_2,chi2_values_4,acceptors_false_value_2] = chi_calculate(donors_false,acceptors_false);
bases = {'a', 'c', 'g', 't'};
donors_father = zeros(18,1);
for i = 1:18
    a = find(chi2_values(:,i) == max(chi2_values(:,i)));
    chi2_values(i,a) = 0;
    if length(a) >1
        continue;
    end
    donors_father(i) = a;
end

donors_father_2 = zeros(18,1);
for i = 1:18
    a = find(chi2_values_3(:,i) == max(chi2_values_3(:,i)));
    chi2_values_3(i,a) = 0;
    if length(a) >1
        continue;
    end
    donors_father_2(i) = a;
end

acceptors_father = zeros(36,1);
for i = 1:36
    a = find(chi2_values_2(:,i) == max(chi2_values_2(:,i)));
    chi2_values_2(i,a) = 0;
    if length(a) >1
        continue;
    end
    acceptors_father(i) = a;
end

acceptors_father_2 = zeros(36,1);
for i = 1:36
    a = find(chi2_values_4(:,i) == max(chi2_values_4(:,i)));
    chi2_values_2(i,a) = 0;
    if length(a) >1
        continue;
    end
    acceptors_father_2(i) = a;
end



[scores_donors,theta_donors,weight_donors] = scores_calculate_donors(donors,donors_father);
[scores_donors_2,theta_donors_2,weight_donors_2] = scores_calculate_donors(donors_false,donors_father_2);
[scores_acceptors,theta_acceptors,weight_acceptors] = scores_calculate_acceptors(acceptors,acceptors_father);
[scores_acceptors_2,theta_acceptors_2,weight_acceptors_2] = scores_calculate_acceptors(acceptors_false,acceptors_father_2);

save('weight_donors','weight_donors')
save('weight_donors_2','weight_donors_2')
save('donors_father','donors_father')
save('donors_father_2','donors_father_2')

save('weight_acceptors','weight_acceptors')
save('weight_acceptors_2','weight_acceptors_2')
save('acceptors_father','acceptors_father')
save('acceptors_father_2','acceptors_father_2')